package com.qiangesoft.rocketmq.producer;

import com.qiangesoft.rocketmq.mode.MqConstant;
import com.qiangesoft.rocketmq.mode.OrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 生产者
 *
 * @author qiangesoft
 * @date 2024-08-22
 */
@Slf4j
@RequestMapping("/rocketMq")
@RestController
public class ProducerController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送简单的消息
     */
    @GetMapping("/sendSimpleMsg")
    public void sendSimpleMsg() {
        SendResult sendResult = rocketMQTemplate.syncSend(MqConstant.TOPIC_TAG, "简单消息同步发送");
        log.info(sendResult.toString());
    }

    /**
     * 发送对象消息
     */
    @GetMapping("/sendObjectMsg")
    public void sendObjectMsg() {
        OrderDTO order = new OrderDTO();
        order.setId(1L);
        order.setOrderNo("YN44239872");
        order.setTitle("张三订单");
        order.setPrice(199.8);
        order.setOrderTime(LocalDateTime.now());

        SendResult sendResult = rocketMQTemplate.syncSend(MqConstant.TOPIC_TAG, order);
        log.info(sendResult.toString());
    }

    /**
     * 发送集合消息
     */
    @GetMapping("/sendCollectionMsg")
    public void sendCollectionMsg() {
        List<OrderDTO> list = new ArrayList<>();

        OrderDTO order = new OrderDTO();
        order.setId(1L);
        order.setOrderNo("YN44239872");
        order.setTitle("张三订单");
        order.setPrice(199.8);
        order.setOrderTime(LocalDateTime.now());
        list.add(order);

        OrderDTO order1 = new OrderDTO();
        order1.setId(2L);
        order1.setOrderNo("YN44239874");
        order1.setTitle("李四订单");
        order1.setPrice(339.0);
        order1.setOrderTime(LocalDateTime.now());
        list.add(order1);

        SendResult sendResult = rocketMQTemplate.syncSend(MqConstant.TOPIC_TAG, list);
        log.info(sendResult.toString());
    }

    /**
     * 发送单向消息
     */
    @GetMapping("/sendOneWay")
    public void sendOneWay() {
        rocketMQTemplate.sendOneWay(MqConstant.TOPIC_TAG, "单向消息");
    }

    /**
     * 发送延迟消息
     */
    @GetMapping("/sendDelayMsg")
    public void sendDelayMsg() {
        Message<String> message = MessageBuilder.withPayload("延迟消息").build();
        // 发送一个延时消息，延迟等级为4级，也就是30s后被监听消费
        SendResult sendResult = rocketMQTemplate.syncSend(MqConstant.TOPIC_TAG, message, 2000, 4);
        log.info(sendResult.toString());
    }

    /**
     * 发送简单消息异步
     */
    @GetMapping("/sendSimpleMsgAsync")
    public void sendSimpleMsgAsync() {
        rocketMQTemplate.asyncSend(MqConstant.TOPIC_TAG, "简单消息异步发送", new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("发送成功");
                log.info(sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                log.error("发送失败");
            }
        });
    }

}
